<template>
  <editTableNew
    :tableSetting="partsDetails"
    :hideBtn="false"
    ref="partsTable"
    @handleClickAdd="addParts"
  ></editTableNew>
</template>

<script>
import { serchEqBackup } from "@/api/equipmentLedger/equipmentBackup";
export default {
  props: {
    spareParts: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      partsTableData: [],
      partsDetails: {
        title: "更换零部件",
        select: false,
        addMethons: true,
        maxHeight: 400,
        btnArr: ["add"], //"edit",
        column: [
          // {
          //   label: "备件名称",
          //   prop: "partName",
          //   required: true,
          //   type: "text"
          // },

          {
            label: "备件名称",
            prop: "partName",
            type: "autocomplete",
            clearable: true,
            required: true,
            changeMethods: (query, item, row) => {
              this.getSpareParts(query, row);
            },
          },
          {
            label: "规格",
            prop: "partModel",
            type: "text",
          },
          {
            label: "使用数量",
            prop: "partNum",
            required: true,
            type: "number",
            min: 1,
            precision: 0,
            step: 1,
          },
          {
            label: "单位",
            prop: "unit",
            type: "text",
            isHide: false,
          },
          {
            label: "单价",
            prop: "unitPrice",
            type: "text",
            min: 1,
            precision: 2,
            step: 1,
            isHide: false,
          },
          {
            label: "总价",
            prop: "totalPrice",
            type: "text",
            min: 1,
            precision: 0,
            step: 1,
            isHide: false,
          },
          {
            label: "品牌库",
            prop: "partBrand", //多选时，字段要为数组类型
            propArr: "brandArr",
            idsStr: "partBrandId",
            option: "brandOption",
            type: "equipmentBrand", //类型
            trigger: "change",
            methods: (obj, row) => {},
          },
          {
            label: "备件来源",
            prop: "partSource",
            type: "select",
            seleteOption: this.$store.getters["dirtData"]("order_part_source"),
            formatter: (val) => {
              return this.$store.getters["fineDirtDataName"](
                "order_part_source",
                val
              );
            },
            methods: (val, row) => {
              if (val == "spare_part") {
                this.getSpareParts("", row);
              }
            },
          },
        ],
        buttonArr: [
          {
            name: "删除",
            isShow: (row, index) => {
              return !this.hideBtn;
            },
            methods: (row, index) => {
              this.handleDelete(row, index);
            },
          },
        ],
      },
    };
  },
  created() {
    this.partsTableData = this.spareParts||[];
  },
  mounted(){
    this.init();
  },
  methods: {
    init() {
      this.partsTableData.map((i) => {
        i.editable = true;
        i.brandOption = this.$utils.getOptionData(i.partBrand, i.partBrandId);
        i.brandArr = i.partBrandId;
      });
      this.$nextTick(() => {
        this.$refs.partsTable.setTableData(this.partsTableData);
      });
    },
    //点击新增零部件
    addParts(data, hadData) {
      let rowIndex = this.partsTableData.rowIndex;
      data.rowIndex = hadData.length + 1;
      data.partNum = 1;
      // data.partSource = "spare_part";
      // data.action = "删除";
      hadData.push(data);
      this.$refs.partsTable.setTableData(hadData);
    },
    //点击删除按钮
    async handleDelete(row, num) {
      let listArr = [];
      let data = await this.$refs.partsTable.getTableData(false);
      data.map((i, index) => {
        if (index != num) {
          i.editable = true;
          listArr.push(i);
        }
      });
      this.$refs.partsTable.setTableData(listArr);
      // return this.$refs.partsTable.deleteItiem(row.rowIndex);
    },
    async getSpareParts(name, row) {
      if (row.partSource == "spare_part") {
        //如果是备件来源是备件备品
        let res = await serchEqBackup({ name });
        let data = res.data.list || [];
        let option = [];
        if (data.length > 0) {
          data.map((i) => {
            let obj = {
              label: i.name,
              value: i.name,
            };
            option.push(obj);
          });
        }
        this.$set(row, "selectOption", option);
      }
    },
    //获取
    getPartsData() {
      return this.$refs.partsTable.getTableData();
    },
  },
};
</script>

<style></style>
